package d5_collection_exception;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 2024/6/5 理解集合的并发修改异常问题,并解决 Cjj 9:56
 */
public class ExceptionTest1 {
    public static void main(String[] args) {

        List<String> list=new ArrayList<>();
        list.add("王麻子");
        list.add("小李子");
        list.add("李爱花");
        list.add("张全蛋");
        list.add("晓李");
        list.add("李玉刚");
        System.out.println(list);

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String name = iterator.next();
            if(name.contains("李")){
                iterator.remove();
            }
        }
        System.out.println(list);
        //需求：找出集合中全部带"李"的名字,并从集合中删除。
    /*    Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String name = it.next();
            if(name.contains("李")){
             list.remove(name);
            }
        }
        System.out.println(list);*/

    /*    for (int i = 0; i < list.size(); i++) {
            String name = list.get(i);
            if(name.contains("李")){
                list.remove(name);
                i--;
            }
        }
        System.out.println(list);*/
    }


}
